package com.luoye.lj.wstool.wsdm;

import com.luoye.lj.wstool.common.StringUtils;
import com.luoye.lj.wstool.generator.ICodeGenerator;
import com.luoye.lj.wstool.web.entity.WebtEntity;
import com.luoye.lj.wstool.web.entity.WebtField;
import com.luoye.lj.wstool.wsdm.entity.WsdtEntity;
import com.luoye.lj.wstool.wsdm.entity.WsdtField;
import com.luoye.lj.wstool.wsdm.entity.WsdtFieldDesc;

/*
 * Created with IntelliJ IDEA.
 * User: Dave Liu
 * Date: 2016/7/7
 * Time: 15:45
 * To change this template use File | Settings | File Templates.
 */
public class HtmlEditTargetCodeGenerator implements ICodeGenerator {
    private WsdtEntity wsdtEntity;

    public HtmlEditTargetCodeGenerator(WsdtEntity wsdtEntity) {
        this.wsdtEntity = wsdtEntity;
    }

    @Override
    public String codeGenerate() {
        String code="";
        if(this.wsdtEntity!=null){
            if(this.wsdtEntity.getFieldList()!=null){
                for(WsdtField wsdtField:this.wsdtEntity.getFieldList()){
                    String type="number";
                    if(wsdtField.getFieldType().equals("Date")){
                        type="datetime-local";
                    }else if(wsdtField.getFieldType().equals("String")){
                        type="text";
                    }
                    if("id".equals(wsdtField.getFieldName())){
                        continue;
                    }
                    if(wsdtField.getWsdtFieldDesc()==null) {
                        code += "                                     <div class=\"form-group\">\n" +
                                "                                        <label class=\"control-label\">" + wsdtField.getDescription() + "：</label>\n" +
                                "                                            <input type=\""+type+"\" class=\"form-control\" id=\"" + wsdtField.getFieldName() + "_edit\" placeholder=\"\"\n" +
                                "                                                   role=\"button\" data-toggle=\"popover\" data-placement=\"bottom\"\n" +
                                "                                                   data-trigger=\"focus\" data-content=\"\">\n" +
                                "                                    </div>\n";
                    }else{
                        if(!wsdtField.getWsdtFieldDesc().isEdit()){//不需要编辑字段
                            continue;
                        }
                        if(wsdtField.getWsdtFieldDesc().getEnumItems()!=null){
                            code += "                                     <div class=\"form-group\">\n" +
                                    "                                        <label class=\"control-label\">" + wsdtField.getWsdtFieldDesc().getText() + "：</label>\n" +
                                    "                                            <select class=\"form-control\" id=\"" + wsdtField.getFieldName() + "_edit\" placeholder=\"\"\n" +
                                    "                                                   role=\"button\" data-toggle=\"popover\" data-placement=\"bottom\"\n" +
                                    "                                                   data-trigger=\"focus\" data-content=\"\">\n";
                            if(!wsdtField.getWsdtFieldDesc().isNotNull()){
                                code += "                        <option value=\"\"></option>\n";
                            }
                            for(WsdtFieldDesc.EnumItem enumItem:wsdtField.getWsdtFieldDesc().getEnumItems()){
                                code+="                        <option value=\""+enumItem.getValue()+"\">"+enumItem.getText()+"</option>\n";
                            }
                            code += "</select>\n" +
                            "                                    </div>\n";
                        }else if(wsdtField.getWsdtFieldDesc().getRelatedEntity()!=null){
                            code += "                                     <div class=\"form-group\">\n" +
                                    "                                        <label class=\"control-label\">" + wsdtField.getWsdtFieldDesc().getText() + "：</label>\n";
                            code += "                                             <select class=\"form-control\" id=\""+wsdtField.getFieldName()+"_edit\">\n";
                            if(!wsdtField.getWsdtFieldDesc().isNotNull()) {
                                code += "                                                 <option value=\"\"></option>\n";
                            }
                            String entityName=wsdtField.getWsdtFieldDesc().getRelatedEntity().getEntityName();
                            String en= StringUtils.makeFirstCharLowercase(entityName);
                            String v=StringUtils.makeFirstCharLowercase(wsdtField.getWsdtFieldDesc().getRelatedEntity().getValueField());
                            String t=StringUtils.makeFirstCharLowercase(wsdtField.getWsdtFieldDesc().getRelatedEntity().getTextField());
                            code += "                                                 <option th:each=\""+en+":\\${"+en+"List}\" th:value=\"\\${"+en+"."+v+"}\" th:text=\"\\${"+en+"."+t+"}\"></option>\n" +
                                    "                                             </select>\n";
                            code += "                                    </div>\n";
                        }else{
                            if(wsdtField.getWsdtFieldDesc().getMaxlength()>0){
                                code += "                                     <div class=\"form-group\">\n" +
                                        "                                        <label class=\"control-label\">" + wsdtField.getWsdtFieldDesc().getText() + "：</label>\n" +
                                        "                                            <input type=\"" + type + "\" class=\"form-control\" id=\"" + wsdtField.getFieldName() + "_edit\" placeholder=\"\"\n" +
                                        "                                                   role=\"button\" data-toggle=\"popover\" data-placement=\"bottom\"\n" +
                                        "                                                   data-trigger=\"focus\" data-content=\"\" maxlength=\""+wsdtField.getWsdtFieldDesc().getMaxlength()+"\">\n" +
                                        "                                    </div>\n";
                            }else {
                                code += "                                     <div class=\"form-group\">\n" +
                                        "                                        <label class=\"control-label\">" + wsdtField.getWsdtFieldDesc().getText() + "：</label>\n" +
                                        "                                            <input type=\"" + type + "\" class=\"form-control\" id=\"" + wsdtField.getFieldName() + "_edit\" placeholder=\"\"\n" +
                                        "                                                   role=\"button\" data-toggle=\"popover\" data-placement=\"bottom\"\n" +
                                        "                                                   data-trigger=\"focus\" data-content=\"\">\n" +
                                        "                                    </div>\n";
                            }
                        }
                    }
                }
            }
        }
        return code;
    }
}
